//
//  SecondViewController.swift
//  BinaryTree
//
//  Created by 郭红旗 on 2021/12/24.
//

import UIKit

class SecondViewController: UIViewController {
    /// 比较器2
    private func comparatorFuncNum(num1:Int,num2:Int) -> Int{
        return num1 - num2
    }
    
    func testFunc() {
        let elements = [55,20,24,15,10,6]
    
//        let manager1 = BinarySearchNodeManager<Int>.init(compare: comparatorFuncNum(num1:num2:))
//        elements.forEach { item in
//            manager1.addNode(withNonRecursive: item)
//        }
//        manager1.printFun()
        
        let manager = DemoSource.init(comparator: comparatorFuncNum(num1: num2:))
        elements.forEach{manager.add(num: $0)}
        manager.printFunc()
        
//        manager.remove(num: 15)
//        manager.printFunc()

    }

    override func viewDidLoad() {
        super.viewDidLoad()
        testFunc()
        return
//        let elements = [55,40,60,24,48,70,20,18,80,90,100]

        /// 二叉搜索树  递归 非递归 创建
        /// 24,9,4,34,2
//        let elements = [11,56,24,9,4,34,2]
        let elements = [55,20,70,0,80,24,67]

        let manager = BinarySearchNodeManager<Int>.init(compare: comparatorFuncNum(num1:num2:))
        elements.forEach { item in
            manager.addNode(withNonRecursive: item)
        }
//        elements.forEach { item in
//            manager.root = manager.addNode(withRecursive: manager.root, value: item)
//        }
        manager.printFun()
//        let _ = manager.flipTree(tree: manager.root!)
//        manager.printFun()
        
//        manager.postorderTraversal(tree: manager.root) { num in
//            print(num)
//        }
        manager.postorderTraversalNonrecursion(tree: manager.root!)

        print("===========")
        /// 二叉搜索平衡树 创建
//        let elements = [55,40,60,24,48,70,20,18]
//        let manager1 = BinaryAVLNodeManager<Int>.init(compare: comparatorFuncNum(num1:num2:))
//        elements.forEach { item in
//            manager1.addNode(withNonRecursive: item)
//        }
//        manager1.addNode(withNonRecursive: 120)
//        manager1.printFun()
//
//        manager1.removeNode(withNonRecursive: 100)
//        manager1.printFun()
//        manager1.removeNode(withNonRecursive: 120)
//        manager1.printFun()
//        manager1.removeNode(withNonRecursive: 60)
//        manager1.printFun()
//        manager1.removeNode(withNonRecursive: 70)
//        manager1.removeNode(withNonRecursive: 90)
//        manager1.printFun()
        
        /// 红黑树树 创建
//        let manager2 = BinaryBRNodeManager<Int>.init(compare: comparatorFuncNum(num1:num2:))
//        manager2.addNode(withNonRecursive: 55)
//        manager2.addNode(withNonRecursive: 20)
//        manager2.addNode(withNonRecursive: 70)
//        manager2.addNode(withNonRecursive: 50)
//        manager2.addNode(withNonRecursive: 52)
//        manager2.addNode(withNonRecursive: 80)
//        manager2.addNode(withNonRecursive: 60)
//        manager2.addNode(withNonRecursive: 90)
//        manager2.addNode(withNonRecursive: 85)
//        manager2.printFun()


    }
    

    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destination.
        // Pass the selected object to the new view controller.
    }
    */

}
